﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using Tulde.WindowsPhone.Music.ViewModel;
using Tulde.WindowsPhone.Music;
using Tulde.WindowsPhone.Music.Mp3Tunes;
using System.Windows.Threading;
using System.Text;

namespace MP3tunes.LockerPlayer.Pages
{
    public partial class VideoPlayer : PhoneApplicationPage
    {
        private double currentVolume;
        private bool isMuted = false;
        private bool isPaused = false;
        DispatcherTimer timer;
    
        

        public bool IsFirstPlay
        {
            get;
            set;
        }
        protected Track SelectedTrack
        {
            get;
            set;
        }
       
        private List<Track> _trackCollection;
        public List<Track> TrackCollection
        {
            get
            {
                if (_trackCollection == null)
                {
                    _trackCollection = new List<Track>();
                }
                return _trackCollection;
            }
            set
            {
                this._trackCollection = value;
                if (this._trackCollection.Any())
                {

                   
                }
            }
        }
      
        

        public void PlayTrack(Track track)
        {
            for (int i = 0; i < this.TrackCollection.Count; i++)
            {
                if (this.TrackCollection[i].TrackId == track.TrackId)
                {
                   
                    this.Play();
                    break;
                }
            }
        }


        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {


            base.OnNavigatedTo(e);
            this.IsFirstPlay = true;
            this.SelectedTrack = NavigationContext.GetViewModelFromQueryParameters<Track>();

            this.nowPlaying.Text = this.SelectedTrack.TrackTitle;

        }
        protected override void OnNavigatingFrom(System.Windows.Navigation.NavigatingCancelEventArgs e)
        {
            base.OnNavigatingFrom(e);
            try
            {
               this.timer.Stop();
               this.player.Stop();
            }
            catch { }
        }

        public VideoPlayer()
        {
            InitializeComponent();
            this.Loaded+=new RoutedEventHandler(MediaPlayer_Loaded);

            timer = new DispatcherTimer();
            timer.Interval = TimeSpan.FromMilliseconds(200);
            timer.Tick += new EventHandler(timer_Tick);

            this.player.MediaEnded += new RoutedEventHandler(player_MediaEnded);
            this.player.MediaFailed += new EventHandler<ExceptionRoutedEventArgs>(player_MediaFailed);
            this.player.MediaOpened += new RoutedEventHandler(player_MediaOpened);
            this.player.BufferingProgressChanged += new RoutedEventHandler(player_BufferingProgressChanged);
            this.player.CurrentStateChanged += new RoutedEventHandler(player_CurrentStateChanged);
            this.player.DownloadProgressChanged += new RoutedEventHandler(player_DownloadProgressChanged);
            player.Volume = 1;
            this.mute.Click += new RoutedEventHandler(mute_Click);
            
           this.SetButtonState(false);
           this.buttonPanel.Visibility = System.Windows.Visibility.Visible;
        }


        void MediaPlayer_Loaded(object sender, RoutedEventArgs e)
        {
            //if (this.SelectedAlbum != null)
            //{
            //    MusicServiceFactory.Instance.GetAlbumTracks(this.SelectedAlbum, tracks =>
            //    {
            //        this.TrackCollection = tracks.Tracks;
            //        this.PlayFirstSong();
            //    });
            //}
            //else if (this.SelectedPlaylist != null)
            //{
            //    MusicServiceFactory.Instance.GetPlaylist(this.SelectedPlaylist, tracks =>
            //    {

            //        var list = new List<Track>();
            //        if (!tracks.Tracks.Empty())
            //        {
            //            list.AddRange(tracks.Tracks);
            //            if (list.Any())
            //            {
            //                this.TrackCollection = list;
            //                this.PlayFirstSong();
            //            }
            //            else
            //            {
            //                this.status.Text = "Found no songs.";
            //                this.progress1.IsIndeterminate = false;
            //                this.SetButtonState(false);
            //            }
            //        }
            //        else
            //        {
            //            this.status.Text = "Found no songs.";
            //            this.progress1.IsIndeterminate = false;
            //            this.SetButtonState(false);
            //        }

            //    });
            //}
            this.volume.Value = this.player.Volume;
            this.Play();
        }
        private void PlayFirstSong()
        {
         }

      
        #region Actions
        void timer_Tick(object sender, EventArgs e)
        {
            this.Dispatcher.BeginInvoke(new Action(() =>
            {

                var p = this.player.Position;
                this.seekBar.Value = this.player.Position.TotalSeconds;
                this.elapsedTxt.Text = this.FormatTime(p);// String.Format("{0}:{1}", p.Minutes, p.Seconds);
            }));

        }
        private void SetButtonState(bool enabled)
        {

            this.play.Visibility = enabled ? System.Windows.Visibility.Visible : System.Windows.Visibility.Collapsed;
            this.next.Visibility = this.play.Visibility;
            this.previous.Visibility = this.play.Visibility;
      
        }
        public void Play()
        {
            if (!Microsoft.Xna.Framework.Media.MediaPlayer.GameHasControl)
            {
                MessageBoxResult result = MessageBox.Show("Media is currently playing in the background.  Continuing will stop all background media.  Continue?", "User Action Required", MessageBoxButton.OKCancel);
                if (result == MessageBoxResult.Cancel)
                {
                    return;
                }
                
            }
            if (!this.isPaused)
            {

                LockerPlayerService s = new LockerPlayerService();
                Uri uri = s.CreateServiceEndpoint(LockerStorageOperations.lockerPlay, new PlayerServiceParameters
                {
                    FileFormat = "h264",
                    Bitrate = Bitrate.b96000,
                    FileKey = this.SelectedTrack.TrackFileKey,
                    FileName = this.SelectedTrack.TrackTitle + ".mpeg"

                });
                this.SetButtonState(true);
              
                this.player.Stop();
                this.play.Visibility = System.Windows.Visibility.Collapsed;
                this.pause.Visibility = System.Windows.Visibility.Visible;
                this.SetProgressBar(true);

                this.status.Text = "Loading " + this.SelectedTrack.TrackTitle;


                this.player.Source = uri;
                // this.player.Play();
            }
            else
            {
                this.isPaused = false;
                this.timer.Start();
                this.play.Visibility = System.Windows.Visibility.Collapsed;
                this.pause.Visibility = System.Windows.Visibility.Visible;
                this.status.Text = "Playing " + this.SelectedTrack.TrackTitle;
                this.player.Play();
            }
        }


        public void Stop()
        {
            this.timer.Stop();
            this.SetProgressBar(false);
            this.play.Visibility = System.Windows.Visibility.Visible;
            this.pause.Visibility = System.Windows.Visibility.Collapsed;
            this.player.Stop();
        }
        public void Pause()
        {
            this.SetProgressBar(false);
            this.timer.Stop();
            this.status.Text = "Paused " + this.SelectedTrack.TrackTitle;
            this.play.Visibility = System.Windows.Visibility.Visible;
            this.pause.Visibility = System.Windows.Visibility.Collapsed;
            this.isPaused = true;
            this.player.Pause();
        }
        public void Next()
        {
            
        }
        public void Previous()
        {
            
              
            
        }
        public void Mute()
        {
            if (this.isMuted)
            {
                // this.volume.Value = this.currentVolume;
                this.player.Volume = this.currentVolume;
                this.isMuted = false;
            }
            else
            {

                this.currentVolume = this.player.Volume;
                this.isMuted = true;
                //this.volume.Value = 0;
                this.player.Volume = 0;
            }
        }

        
        void player_BufferingStarted(object sender, RoutedEventArgs e)
        {
         
        }


        void mute_MouseUp(object sender, MouseButtonEventArgs e)
        {

            this.Mute();
        }


        void player_BufferingProgressChanged(object sender, RoutedEventArgs e)
        {

        }
        void player_MediaOpened(object sender, RoutedEventArgs e)
        {
            this.buttonPanel.Visibility = System.Windows.Visibility.Visible;

            if (this.player.NaturalDuration.HasTimeSpan)
            {
                TimeSpan ts = this.player.NaturalDuration.TimeSpan;
                seekBar.Maximum = ts.TotalSeconds;
                seekBar.SmallChange = 1;
                seekBar.LargeChange = Math.Min(10, ts.Seconds / 10);

                //     this.status.Text = "Playing " + this.CurrentTrack.ArtistName + " - " + this.CurrentTrack.TrackTitle;
                //     this.OnTrackPlay(this.CurrentTrack);
            }
            if (!this.IsFirstPlay)
            {


                // timer.Start();

                this.status.Text = "Playing " + this.SelectedTrack.TrackTitle;

            }
            else
            {
                this.IsFirstPlay = false;
                this.player.Pause();
                this.player.Play();
                this.status.Text = "Playing " + this.SelectedTrack.TrackTitle;
         
            }
           

            if (this.player.NaturalDuration.HasTimeSpan)
            {
                // this.durationTxt.Text = string.Format("{0:mm\\:ss}", player.NaturalDuration.TimeSpan);
                this.durationTxt.Text = this.FormatTime(player.NaturalDuration.TimeSpan); // String.Format("{0}:{1}", player.NaturalDuration.TimeSpan.Minutes, player.NaturalDuration.TimeSpan.Seconds);
                timer.Start();
            }
        }

        void mute_Click(object sender, RoutedEventArgs e)
        {
            this.Mute();
        }
        void player_DownloadProgressChanged(object sender, RoutedEventArgs e)
        {

        }

        void player_CurrentStateChanged(object sender, RoutedEventArgs e)
        {

        }
        void player_MediaEnded(object sender, RoutedEventArgs e)
        {
            this.Next();
        }

        void player_MediaFailed(object sender, ExceptionRoutedEventArgs e)
        {
            this.status.Text = e.ErrorException.Message;
        }
        public void SetProgressBar(bool state)
        {
            this.progress1.IsIndeterminate = state;
            this.progress1.IsEnabled = state;
        }
        #endregion

        private string FormatTime(TimeSpan ts)
        {
            int hour = ts.Hours;
            int minute = ts.Minutes;
            int seconds = ts.Seconds;

            StringBuilder b = new StringBuilder();
            b.Append(hour < 10 ? "0" + hour : hour.ToString());
            b.Append(":");
            b.Append(minute < 10 ? "0" + minute : minute.ToString());
            b.Append(":");
            b.Append(seconds < 10 ? "0" + seconds : seconds.ToString());
            return b.ToString();
        }

        private void previous_Click(object sender, RoutedEventArgs e)
        {
            this.Previous();
        }

        private void play_Click(object sender, RoutedEventArgs e)
        {
            this.Play();
        }

        private void pause_Click(object sender, RoutedEventArgs e)
        {
            this.Pause();
        }

        private void next_Click(object sender, RoutedEventArgs e)
        {
            this.Next();
        }

        private void volume_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
            this.player.Volume = e.NewValue;
        }

        private void volume_ManipulationCompleted(object sender, ManipulationCompletedEventArgs e)
        {
            this.player.Volume = this.volume.Value;
        }
    }
   
}